package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.communal.PublicReturn;
import com.enhe.gck.item.OrderHWANE16677Item;
import com.enhe.gck.item.PouringOrderHWANE18490Item;
import com.enhe.gck.pojo.Car;
import com.enhe.gck.pojo.Customer;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import com.kuangkie.carbon.fg.FuncGroupContext;
import com.kuangkie.carbon.fg.ImproveResultFactory;
import com.kuangkie.carbon.fg.SecondRoundImproveFuncGroup;
import com.enhe.gck.CarbonBaseConstant; //模型Code
import com.enhe.gck.CarbonEnumKeyValue; //枚举字典信息
import com.enhe.gck.CarbonRelationType; //关系类型Code
import com.enhe.gck.item.CarHWANE16654Item; // 模型属性Code

import java.util.Collection;
import java.util.List;

@Slf4j
@Repository(value = "hwane16654366042330866425862")
public class CarHWANE16654BNB implements SecondRoundImproveFuncGroup{

	@Override
	public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		Car info=proRecord.getBean(Car.class);

		if(info.getIsDelete()!=null&&!info.getIsDelete()){
			String regex = "[^ \\//\\\\]+";
			if (!info.getCarNo().matches(regex)) {
				PublicMethod.returnMessage(improveResultFactory, PublicReturn.Input_1);
				return;
			}

			if(isHave(info,improveResultFactory,proRecord)){
				return;
			}
		}

		if(proRecord.isNew()){
			improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(CarHWANE16654Item.基本属性组_删除,false);

			if(String.valueOf(CarbonEnumKeyValue.操作命令_添加当前账户车辆_tjdqzhcl).equals(info.getCommand())){
				log.info("添加当前账户车辆");
				if(context.getUserCode()==null){
					PublicMethod.returnMessage(improveResultFactory, PublicReturn.USER_1);
					return;
				}
				RecordQueryProConJunctionFactory query= CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.客户管理_khgl).getProConJunctionFactory();
				query.getRelationCriterionFactory(CarbonBaseConstant.系统用户_xtyh)
						.setInRelationTypes(CarbonRelationType.RR_客户管理_账户_系统用户)
						.setInGuestCodes(context.getUserCode());
				List<String> list=query.goBack().queryCodes();
				if(list==null|| list.isEmpty()){
					PublicMethod.returnMessage(improveResultFactory, PublicReturn.USER_2);
					return;
				}
				for (String rId:list) {
					improveResultFactory.getCurrentProRecordOpsBuilder().addRelation(CarbonRelationType.RR_车辆管理_客户名称_客户管理,rId);
				}
			}
		}
	}

	private boolean isHave(Car info ,ImproveResultFactory improveResultFactory,ProRecord proRecord){
		boolean isOk=false;
		List<Car> carOldList=PublicMethod.query(CarbonBaseConstant.车辆管理_clgl)
				.addEqual(CarHWANE16654Item.基本属性组_车牌号,info.getCarNo())
				.addEqual(CarHWANE16654Item.基本属性组_删除,false)
				.goBack().queryBeans(Car.class,1);
		;
		if(carOldList!=null&& !carOldList.isEmpty()){

			if(proRecord.isNew()){
				Collection<String> khList=proRecord.getGuestRecordCodes(CarbonRelationType.RR_车辆管理_客户名称_客户管理);
				for (Car car : carOldList) {
					if(khList!=null&&!khList.isEmpty()){
						FGRecordComplexusBuilder fGRecordComplexusBuilder=CarbonPanel.getFGRecordComplexusBuilder();
						ProRecordBuilder proRecordBuilder=fGRecordComplexusBuilder.getProRecordBuilder(CarbonBaseConstant.车辆管理_clgl,car.getRecordCode());
						for (String s : khList) {
							proRecordBuilder.addRelation(CarbonRelationType.RR_车辆管理_客户名称_客户管理,s);
						}
						IntegrationMsg msg=CarbonPanel.getRecordIntegration(CarbonBaseConstant.车辆管理_clgl).integrate(fGRecordComplexusBuilder.getFGRecordComplexus());
						if(!msg.success()){
							log.info("修改车辆关联失败:{}",msg.getRefuseMsgContentStr());
						}
					}
				}
				improveResultFactory.getCurrentProRecordOpsBuilder().setNotPersist(true);
//				isOk=true;
//				PublicMethod.returnMessage(improveResultFactory, PublicReturn.Car_1);
			}else {
				boolean isOkHave=false;
				for (Car car : carOldList) {
					if(!car.getRecordCode().equals(info.getRecordCode())){
						isOk=true;

					}else {
						isOkHave=true;
					}
				}
				if(isOk&&!isOkHave){
//					PublicMethod.returnMessage(improveResultFactory, PublicReturn.Car_1);
					isOk=false;
					improveResultFactory.getCurrentProRecordOpsBuilder().setNotPersist(true);
				}else {
					isOk=false;
				}
			}
		}


		return isOk;
	}
}
